// =============================================
// FD Luma Color Remap
// Made by Ubik and Claude 2026
// =============================================
// Maps image luminance to a 2-point color
// gradient (shadow -> highlight). Original
// colors replaced with a custom palette.
// Connect video source to video in 1.
//
// Note: Isadora has no native color picker
// parameter type. Shadow and highlight colors
// are set via R/G/B sliders. Tip: use a
// Color Calculator actor to preview colors.
// =============================================

// ISADORA_PLUGIN_DESC("Luma Color Remap - maps luminance to a 2-point gradient. Set shadow and highlight colors via R/G/B sliders.")

// ISADORA_FLOAT_PARAM(shadow_r, shr, 0.0, 1.0, 0.05, "SHADOW color - Red. Dark areas of the image will take this color.")
// ISADORA_FLOAT_PARAM(shadow_g, shg, 0.0, 1.0, 0.0, "SHADOW color - Green.")
// ISADORA_FLOAT_PARAM(shadow_b, shb, 0.0, 1.0, 0.18, "SHADOW color - Blue.")
// ISADORA_FLOAT_PARAM(highlight_r, hlr, 0.0, 1.0, 0.9, "HIGHLIGHT color - Red. Bright areas of the image will take this color.")
// ISADORA_FLOAT_PARAM(highlight_g, hlg, 0.0, 1.0, 1.0, "HIGHLIGHT color - Green.")
// ISADORA_FLOAT_PARAM(highlight_b, hlb, 0.0, 1.0, 0.4, "HIGHLIGHT color - Blue.")
// ISADORA_FLOAT_PARAM(remap_strength, rmst, 0.0, 1.0, 1.0, "Blend between original color (0) and full remap (1).")
// ISADORA_FLOAT_PARAM(contrast, cont, 0.5, 3.0, 1.0, "Sharpens the luminance curve before remapping. High = harder separation.")

uniform float shadow_r;
uniform float shadow_g;
uniform float shadow_b;
uniform float highlight_r;
uniform float highlight_g;
uniform float highlight_b;
uniform float remap_strength;
uniform float contrast;
uniform vec3 iResolution;
uniform sampler2D tex0;

void main()
{
    vec2 uv = gl_FragCoord.xy / iResolution.xy;
    vec4 original = texture2D(tex0, uv);

    // Perceptually weighted luminance
    float luma = dot(original.rgb, vec3(0.299, 0.587, 0.114));

    // Apply contrast around midpoint
    luma = clamp((clamp(luma, 0.0, 1.0) - 0.5) * contrast + 0.5, 0.0, 1.0);

    // 2-point gradient: shadow -> highlight
    vec3 remapped = mix(
        vec3(shadow_r, shadow_g, shadow_b),
        vec3(highlight_r, highlight_g, highlight_b),
        luma
    );

    vec3 result = mix(original.rgb, remapped, remap_strength);
    gl_FragColor = vec4(result, original.a);
}
